const express = require("express");
const app = express();
const cors = require("cors");

const xhjsbRouter = require("./router/xhjsb");
const mjjcRouter = require("./router/mjjc");

//解决跨域问题
app.use(cors());
//添加解析json数据的中间件,必须在路由之前
app.use(express.json());
//添加解析数据表单的中间件
app.use(express.urlencoded({ extended: false }));

//优化：自定义封装res.cc函数
app.use((req, res, next) => {
  res.cc = function (err, status = 1) {
    res.send({
      status,
      message: err instanceof Error ? err.message : err,
    });
  };
  next();
});

//添加小黑记事本的模块
app.use("/xh", xhjsbRouter);

//添加解析面经基础的模块
app.use("/mj", mjjcRouter);

//添加错误中间件
app.use((err, req, res, next) => {
  if (err) return res.cc(err);
});

app.listen(80, () => {
  console.log("express server is running at 127.0.0.1");
});
